package com.hyts.dbutils.executor;

import com.hyts.dbutils.base.crud.InsertSqlExecutor;
import com.hyts.dbutils.base.crud.SelectSqlExecutor;
import com.hyts.dbutils.bean.dbutils.DbConnectionBean;
import com.hyts.dbutils.bean.dbutils.DbParameterBean;
import lombok.Cleanup;
import lombok.SneakyThrows;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;


/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.dbopera.dbutils.executor
 * author:Libo/Alex
 * create-date:2019-06-23 21:10
 * copyright:libo-hyts-github
 * email:libo2dev@aliyun.com
 * description:此类主要用于: dbUtils工具类操作服务数据操作执行器
 */
public class DBUtilsExecutor<T,R> implements InsertSqlExecutor<DbParameterBean<T>,R>, SelectSqlExecutor<DbParameterBean<T>,R> {

    /**
     * 数据库链接对象
     */
    private DbConnectionBean dbConnBean;

    /**
     * 构建对象构造器
     * @param dbConnBean
     */
    public DBUtilsExecutor(DbConnectionBean dbConnBean) {
        this.dbConnBean = build(dbConnBean);
    }

    /**
     * 插入方法操作
     * @param param
     * @return
     */
    @SneakyThrows(SQLException.class)
    @Override
    public R insert(DbParameterBean<T> param, Class<R> resultType) {
        QueryRunner queryRunner = (QueryRunner) dbConnBean.getQueryRunner();
        @Cleanup Connection connection = this.openConnection(dbConnBean);
        return queryRunner.insert(connection,param.getSql(), new BeanHandler<>(resultType),param.getParams()[0]);
    }

    /**
     * 批量插入方法操作
     * @param param
     * @return
     */
    @SneakyThrows(SQLException.class)
    @Override
    public R insertBatch(DbParameterBean<T> param, Class<R> resultType) {
        QueryRunner queryRunner = (QueryRunner) dbConnBean.getQueryRunner();
        @Cleanup Connection connection = this.openConnection(dbConnBean);
        return queryRunner.insertBatch(connection,param.getSql(), new BeanHandler<R>(resultType),param.getParams());
    }

    /**
     * 查询数据单条
     * @param param
     * @param returnClass
     * @return
     */
    @SneakyThrows(SQLException.class)
    @Override
    public R selectOne(DbParameterBean<T> param, Class<R> returnClass) {
        QueryRunner queryRunner = (QueryRunner) dbConnBean.getQueryRunner();
        @Cleanup Connection connection = this.openConnection(dbConnBean);
        return queryRunner.query(connection,param.getSql(), new BeanHandler<>(returnClass), Objects.isNull(param.getParams()) ? null:param.getParams()[0]);
    }

    /**
     * 操作服务
     * @param param
     * @param returnClass
     * @return
     */
    @SneakyThrows(SQLException.class)
    @Override
    public List<R> selectList(DbParameterBean<T> param, Class<R> returnClass) {
        QueryRunner queryRunner = (QueryRunner) dbConnBean.getQueryRunner();
        @Cleanup Connection connection = this.openConnection(dbConnBean);
        return queryRunner.query(connection,param.getSql(), new BeanListHandler<>(returnClass), Objects.isNull(param.getParams()) ? null:param.getParams()[0]);
    }
}
